package com.you.concurrent.thread.interrupt;

public class ThreadInterruptDemo {

    static int num = 0;

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            while (true) {
                System.out.println(Thread.currentThread().isInterrupted());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) { //InterruptedException会对线程中断标识复位
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().isInterrupted());
                num++;
                if (Thread.currentThread().isInterrupted()) { //判断线程是否被中断
                    System.out.println(Thread.currentThread().isInterrupted());
                    Thread.interrupted(); //自我恢复
                    System.out.println(Thread.currentThread().isInterrupted());
                    System.out.println(Thread.currentThread().getName() + "----is end!");
                    System.out.println("num is=" + num);
                    break;
                }
            }
        }, "线程1");
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.start();
        try {
            Thread.sleep(10L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt(); //对某个线程进行中断通知
    }
}
